<?php
/*
	Youtube version 2 
	video upload class and code 
	
	// Youtube constants
	defined( 'YOUTUBE_API_KEY' )
		|| define( 'YOUTUBE_API_KEY', "AI39si4ep9yC2ca5L-1W092OI1b0UVfXuSq7jo5SQkGSlrtexvyHBgQUweHf1Sq5Dmmn9YToC1KqTeMR8KR08itfS_mJ6v0RDA" );
	defined( 'YOUTUBE_USER' )
		|| define( 'YOUTUBE_USER', 'pramod@contentcloudhq.com' );
	defined( 'YOUTUBE_PASSWORD' )
		|| define( 'YOUTUBE_PASSWORD', 'shimla123' );
	defined( 'YOUTUBE_APPLICATION_NAME' )
		|| define( 'YOUTUBE_APPLICATION_NAME', 'Qyuki Test ipr4' );
		
	
	$objYoutube = new Youtube_Youtube();
	$result = $objYoutube->upload( $approvedCreationObj->getTitle(), $approvedCreationObj->getSlug(),
	$approvedCreationObj->getContent(), 'Animals',
	$approvedCreationObj->getTags(), $video_path, 'video/mp4' );
								
 */

	class Youtube_Youtube {
		protected $_youtubeClient = false;

		function __construct() {
			$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
				$username = YOUTUBE_USER,
				$password = YOUTUBE_PASSWORD,
				$service = 'youtube',
				$client = null,
				$source = YOUTUBE_APPLICATION_NAME,
				$loginToken = null,
				$loginCaptcha = null,
				'https://www.google.com/accounts/ClientLogin'
			);

			$this->_youtubeClient = new Zend_Gdata_YouTube( $httpClient, YOUTUBE_APPLICATION_NAME, NULL, YOUTUBE_API_KEY );
			$this->_youtubeClient->setMajorProtocolVersion( 2 );			
		}

		public function upload( $title, $slug, $description, $category, $tags, $video_path, 
								$mineType, $location = '37.0 -122.0' ) {

			$newVideoEntry = new Zend_Gdata_YouTube_VideoEntry();

			//media details
			$filesource = $this->_youtubeClient->newMediaFileSource( $video_path );
			$filesource->setContentType( $mineType );
			$filesource->setSlug( $slug );
			$newVideoEntry->setMediaSource( $filesource );

			$newVideoEntry->setVideoTitle( $title );
			$newVideoEntry->setVideoDescription( $description );
			$newVideoEntry->setVideoCategory( $category );
			$newVideoEntry->SetVideoTags( $tags );
			
			//add location to video
			$this->_youtubeClient->registerPackage( 'Zend_Gdata_Geo' );
			$this->_youtubeClient->registerPackage( 'Zend_Gdata_Geo_Extension' );			
			$where = $this->_youtubeClient->newGeoRssWhere();
			$position = $this->_youtubeClient->newGmlPos( $location );
			$where->point = $this->_youtubeClient->newGmlPoint( $position );

			$newVideoEntry->setWhere( $where );

			$uploadUrl = 'http://uploads.gdata.youtube.com/feeds/api/users/default/uploads';
			try {
				$newEntry = $this->_youtubeClient->insertEntry( $newVideoEntry, $uploadUrl, 'Zend_Gdata_YouTube_VideoEntry' );
				return $newEntry;
			} catch( Zend_Gdata_App_HttpException $httpException ) {
				describe( $httpException->getMessage(), $httpException->getTraceAsString() );
				echo $httpException->getRawResponseBody();
			} catch( Zend_Gdata_App_Exception $ex ) {
				describe( $ex->getMessage(), $ex->getTraceAsString() );
				echo $ex->getMessage();
			}
			return false;
		}

	}